package cn.iocoder.yudao.module.dr.service.journal;

import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.JournalPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.JournalSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.journal.JournalDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dr.dal.mysql.journal.JournalMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.*;

/**
 * 期刊信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class JournalServiceImpl implements JournalService {

    @Resource
    private JournalMapper journalMapper;

    @Override
    public Long createJournal(JournalSaveReqVO createReqVO) {
        // 插入
        JournalDO journal = BeanUtils.toBean(createReqVO, JournalDO.class);
        journalMapper.insert(journal);
        // 返回
        return journal.getYwid();
    }

    @Override
    public void updateJournal(JournalSaveReqVO updateReqVO) {
        // 校验存在
        validateJournalExists(updateReqVO.getYwid());
        // 更新
        JournalDO updateObj = BeanUtils.toBean(updateReqVO, JournalDO.class);
        journalMapper.updateById(updateObj);
    }

    @Override
    public void deleteJournal(Long id) {
        // 校验存在
        validateJournalExists(id);
        // 删除
        journalMapper.deleteById(id);
    }

    private void validateJournalExists(Long id) {
        if (journalMapper.selectById(id) == null) {
            throw exception(JOURNAL_NOT_EXISTS);
        }
    }

    @Override
    public JournalDO getJournal(Long id) {
        return journalMapper.selectById(id);
    }

    @Override
    public PageResult<JournalDO> getJournalPage(JournalPageReqVO pageReqVO) {
        return journalMapper.selectPage(pageReqVO);
    }

    @Override
    public List<JournalDO> queryByIds(List<Long> list) {
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return journalMapper.queryByIds(list);
    }

    @Override
    public PageResult<JournalDO> selectApply(JournalPageReqVO pageReqVO) {
        Page<JournalDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<JournalDO> selectList = journalMapper.selectApply(page,pageReqVO);
        PageResult<JournalDO> result = new PageResult<>();
        result.setTotal(selectList.getTotal());
        result.setList(selectList.getRecords());
        return result;
    }

}